package com.ynet.middleground.user.service.impl;

import java.util.List;

import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Throwables;
import com.ynet.core.common.BaseReqObjPager;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.user.bean.AddChannelReq;
import com.ynet.middleground.user.bean.QueryBaseReq;
import com.ynet.middleground.user.bean.QueryChannelByCodeReq;
import com.ynet.middleground.user.bean.UpdateChannelReq;
import com.ynet.middleground.user.bean.searchAllChannelsReq;
import com.ynet.middleground.user.common.ErrorMessageConstant;
import com.ynet.middleground.user.dto.ChannelDTO;
import com.ynet.middleground.user.model.ChannelConfigModel;
import com.ynet.middleground.user.service.ChannelService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @author liulx
 */
@Api(value = "渠道管理服务", tags = {"渠道管理服务"}, hidden = false)
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Consumes({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Path("channel")
@Service(timeout = 6000, version = "0.0.1")
public class ChannelServiceImpl implements ChannelService, GenericService {

    @Autowired
    private ChannelConfigModel channelConfigModel;

    /**
     * 查询所有渠道信息
     *
     * @return 所有渠道列表
     */
    @ApiOperation(value = "查询所有渠道信息", notes = "查询所有渠道信息", hidden = false)
    @POST
    @Path("all-channel")
    @ApiImplicitParam(name = "requestObj", dataType = "searchAllChannelsReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<ChannelDTO>> searchAllChannels(searchAllChannelsReq requestObj) {
        ServiceResult<List<ChannelDTO>> result = new ServiceResult<>();
        try {
            result.setResult(channelConfigModel.listAlls(requestObj));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "查询所有渠道信息服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

    /**
     * 查询所有渠道信息
     *
     * @return 所有渠道列表
     */
    @ApiOperation(value = "查询所有渠道信息(内管使用)", notes = "查询所有渠道信息", hidden = true)
    @POST
    @Override
    public ServiceResult<List<ChannelDTO>> searchAllChannel() {
        ServiceResult<List<ChannelDTO>> result = new ServiceResult<>();
        try {
            result.setResult(channelConfigModel.listAll());
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "查询所有渠道信息服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }
    
    /**
     * 分页查询渠道
     *
     * @param req 基础参数分页
     * @return 渠道列表
     */
    @ApiOperation(value = "分页查询渠道(内管使用)", notes = "分页查询渠道", hidden = true)
    @Override
    public ServiceResult<List<ChannelDTO>> listPage(BaseReqObjPager req) {
        ServiceResult<List<ChannelDTO>> result = new ServiceResult<>();
        try {
            result.setResult(channelConfigModel.listPage(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "分页查询渠道服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        result.setPagerInfo(req.getPagerInfo());
        return result;
    }

    /**
     * 保存渠道
     *
     * @param req 新增渠道请求对象
     * @return 保存成功后的渠道 id
     */
    @ApiOperation(value = "保存渠道(内管使用)", notes = "保存渠道", hidden = true)
    @Override
    public ServiceResult<Integer> addChannel(@Valid AddChannelReq req) {
        ServiceResult<Integer> result = new ServiceResult<>();
        try {
            result.setResult(channelConfigModel.saveChannel(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "保存渠道服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

    /**
     * 根据渠道 id 查询渠道信息
     *
     * @param req 查询基础对象
     * @return 渠道信息传输对象
     */
    @ApiOperation(value = "查询渠道详情(内管使用)", notes = "查询渠道详情", hidden = true)
    @Override
    public ServiceResult<ChannelDTO> queryDetail(@Valid QueryBaseReq req) {
        ServiceResult<ChannelDTO> result = new ServiceResult<>();
        try {
            result.setResult(channelConfigModel.getDetailById(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "查询渠道详情服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

    /**
     * 更新渠道信息
     *
     * @param req 修改渠道信息请求对象
     * @return 结果以 success 字段为准, true 为修改成功， false 为修改失败
     */
    @ApiOperation(value = "更新渠道信息(内管使用)", notes = "更新渠道信息", hidden = true)
    @Override
    public ServiceResult<Object> modify(@Valid UpdateChannelReq req) {
        ServiceResult<Object> result = new ServiceResult<>();
        try {
            channelConfigModel.modify(req);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "更新渠道信息服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

    /**
     * 根据渠号查询渠道信息请求对象
     *
     * @param req 根据渠号查询渠道信息请求对象
     * @return 渠道信息传输对象
     */
    @ApiOperation(value = "根据渠号查询渠道信息(内管使用)", notes = "根据渠号查询渠道信息", hidden = true)
    @Override
    public ServiceResult<ChannelDTO> queryChannelByCode(@Valid QueryChannelByCodeReq req) {
        ServiceResult<ChannelDTO> result = new ServiceResult<>();
        try {
            result.setResult(channelConfigModel.getDetailByCode(req.getChannelCode()));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "根据渠号查询渠道信息服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

}
